import { Component, OnInit } from '@angular/core';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService,NzModalService,UploadFile,UploadFilter } from 'ng-zorro-antd';

@Component({
  selector: 'app-wxpay',
  templateUrl: './wxpay.component.html',
  styleUrls: ['./wxpay.component.css']
})
export class WxpayComponent implements OnInit {

  isLoading=false;
  public logoUrl:string="assets/images/none.jpg";
  public QRcode=host+"/plupload";
  public values: Array<any> = [];
  public fileList=[];
  public onChanges(values: any): void {
    this.values=values;
  }

  constructor(private heroService: HeroService,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  ngOnInit() {
    // 回显二维码
    let url=host+"/main/merchantPayCode/view?type=0";
    this.heroService.get(url).then(result=>{
        if(result.code==0){
          if(result.data.bean.url){
            this.logoUrl = result.data.bean.url;
          }
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }

  // 筛选判断上传图片格式
  filters: UploadFilter[] = [
    {
      name: 'type',
      fn  : (fileList: UploadFile[]) => {
        const filterFiles = fileList.filter(w => ~['image/png','image/jpeg','image/gif','image/bmp'].indexOf(w.type));
        if (filterFiles.length !== fileList.length) {
          this.message.error(`只能上传图片`);
          return filterFiles;
        }
        return fileList;
      }
    }
  ];
  // 获取上传logo图片返回信息
  logoChange(info: any): void {
    this.isLoading = true;
    if (info.type=="success") {
      info.file.url = info.file.response.url;
      this.logoUrl = info.file.url;
      //确认上传
      let url=host+"/main/merchantPayCode/save";
      let model="type=0&status=0"+"&url="+this.logoUrl+"&name="+info.file.response.originalName+"&size="+info.file.response.size;
      this.heroService.post(url,model).then(result=>{
          if(result.code==0){
            this.isLoading = false;
            this.message.success('上传成功'); 
          }else{
            this.isLoading = false;
            this.modalService.error({
              nzTitle: '提示',
              nzContent: result.msg
            });
          }
      })
    }
    if (info.type=="removed") {
      this.message.warning('删除成功'); 
    }
  }
}
